package com.tourscool.springboot.starter.inquiry.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * Http请求类
 * @author Cedric
 */
@Slf4j
public class HttpRequestUtils {

	private static CloseableHttpClient httpClient;

	static {
		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
		cm.setMaxTotal(100);
		cm.setDefaultMaxPerRoute(20);
		cm.setDefaultMaxPerRoute(50);
		httpClient = HttpClients.custom().setConnectionManager(cm).build();
	}

	public static Boolean option(String url) {
		CloseableHttpResponse response = null;
		try {
			HttpOptions httpOptions = new HttpOptions(url);
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)
					.setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
			httpOptions.setConfig(requestConfig);
			httpOptions.addHeader("Content-type", "application/json; charset=utf-8");
			httpOptions.setHeader("Accept", "application/json");
			response = httpClient.execute(httpOptions);
			return response.getStatusLine().getStatusCode() == 200;
		} catch (IOException ignore) {

		} finally {
			try {
				if (null != response) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return Boolean.FALSE;
	}

	/**
	 * post请求
	 * @param url 请求链接
	 * @param jsonString 请求字符串
	 * @return 响应信息
	 */
	public static String post(String url, String jsonString) {
		log.info("url={}, jsonString={}", url, jsonString);
		CloseableHttpResponse response = null;
		BufferedReader in;
		String result = "";
		try {
			HttpPost httpPost = new HttpPost(url);
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)
					.setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
			httpPost.setConfig(requestConfig);
			httpPost.addHeader("Content-type", "application/json; charset=utf-8");
			httpPost.setHeader("Accept", "application/json");
			httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
			response = httpClient.execute(httpPost);
			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			StringBuilder sb = new StringBuilder();
			String line;
			String nl = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line).append(nl);
			}
			in.close();
			result = sb.toString().replace("\r\n", "");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != response) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

}
